import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JLabel;


public class AdminServices extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public AdminServices(String user) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(Color.ORANGE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnAddNewCourse = new JButton("Add New Course");
		btnAddNewCourse.setBounds(158, 66, 130, 29);
		contentPane.add(btnAddNewCourse);
		
		JButton btnViewAdminReport = new JButton("View Admin Report");
		btnViewAdminReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				UIManager.put("OptionPane.background", Color.ORANGE);
			
				String nam = JOptionPane.showInputDialog (getContentPane(),"<html><b>Enter Course Code</b></html>");
				Connection con = Connect.getConnection();
				Statement stat=con.createStatement();
				if(nam.equals("") || nam.trim().equals("")){
					JOptionPane.showMessageDialog(getContentPane(),"Enter a Course Code");
				}
				else{
					double cum=0;
					Boolean check=false;
					String gr="";
					
					ResultSet rs= stat.executeQuery("SELECT R.Grade, COUNT(R.Grade),S.Course_Title FROM CRS_CODE AS C, SECTION AS S, REGISTERS AS R WHERE C.Code = '"+nam+"' AND C.Course_Title = S.Course_Title AND S.CRN = R.Section_CRN AND Grade<>'INCOMPLETE' AND Grade_Mode='GRADE'  GROUP BY R.Grade");
					double avg=0.0;
					double total=0.0;
					String savg="-";
					String course_title="";
					while(rs.next()){
						check=true;
						System.out.println(check);
						gr=rs.getString(1);
						if(gr.equals("A")){
							cum+=(4*rs.getInt(2));
							total+=(4*rs.getInt(2));
						}else if(gr.equals("B")){
							cum+=(3*rs.getInt(2));
							total+=(4*rs.getInt(2));
						}else if(gr.equals("C")){
							cum+=(3*rs.getInt(2));
							total+=(4*rs.getInt(2));
						}else if(gr.equals("D")){
							cum+=(3*rs.getInt(2));
							total+=(4*rs.getInt(2));
						}
						course_title=rs.getString(3);
							
				    }
					Icon i=new ImageIcon(FacultyServices.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg"));
					
					System.out.println(check);
					if (check){
						avg=(cum/total)*4;
						
						JOptionPane.showMessageDialog(getContentPane(),"Course_code: '"+nam +"'\n Course Title: '" + course_title +"'\n Average GPA: '" + avg +"'","Admin Report",JOptionPane.INFORMATION_MESSAGE,i);
					}
					else{
						JOptionPane.showMessageDialog(getContentPane(),"No Result","Admin Report",JOptionPane.INFORMATION_MESSAGE,i);
					}
				}
				}catch(Exception h){
					JOptionPane.showMessageDialog(contentPane,"Check Connection");	
				}
				}
		});
		btnViewAdminReport.setBounds(158, 127, 144, 29);
		contentPane.add(btnViewAdminReport);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(AdminServices.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg")));
		label.setBounds(512, 41, 144, 149);
		contentPane.add(label);
		setPreferredSize(new Dimension(700, 300));
		pack();
		setVisible(true);
		setTitle("Admin Services");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
